2
2
.
.
7
7
.
.
5
5
@
@
M
M
a
a
n
n
y
y
T
T
o
o
M
M
a
a
n
n
y
y
-
-
J
J
o
o
i
i
n
n
T
T
a
a
b
b
l
l
e
e
I
I
n
n
f
f
o
o
[
[
G
G
]
]
[
[
R
R
]
]
This tutorial shows how to implement @ManyToMany relationship by using Join-Table.
@ManyToMany relationship can't be implemented using Foreign-Key approach.
Application Schema [Results]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables @Controller and @RequestMapping. Includes Tomcat Server.
SQL
Spring Data JPA
Enables @Entity and @Id
SQL
H2 Database
Enables in-memory H2 Database
DB Schema
Syntax
@OneToMany(cascade = CascadeType.ALL) //By default uses Join-Table
@JoinColumn(name = "authorId") //Overrides default behaviour by using Foreign-Key in Book
public Set<Book> books;
AUTHOR
NAME
AGE
BOOK
ID
BOOKS_ID
AUTHOR_BOOKS
AUTHOR_ID
MyController
http://localhost:8080/AddAuthor
Author
AuthorRepository
addAuthor()
Book
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: springboot_relationships_manytoone_jointable (add Spring Boot Starters from the table)
Edit FIle: application.properties (specify H2 DB name & enable H2 Web Console)
Create Package: entities (inside main package)
– Create Class: Author.java (inside package entities)
– Create Class: Book.java (inside package entities)
Create Package: repositories (inside main package)
– Create Interface: AuthorRepository.java (inside package repositories)
Create Package: controllers (inside main package)
– Create Class: MyController.java (inside package controllers)
application.properties
spring.datasource.url = jdbc:h2:mem:testdb
spring.h2.console.enabled = true
AuthorRepository.java
package com.ivoronline.springboot_relationships_manytoone_jointable.repositories;
import com.ivoronline.springboot_relationships_manytoone_jointable.entities.Author;
import org.springframework.data.repository.CrudRepository;
public interface AuthorRepository extends CrudRepository<Author, Integer> { }
Author.java
package com.ivoronline.springboot_relationships_manytoone_jointable.entities;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.ManyToMany;
import javax.persistence.CascadeType;
import java.util.Set;
@Entity
public class Author {
//PRIMARY KEY
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer id;
//RELATIONSHIP
@ManyToMany(cascade = CascadeType.ALL)
public Set<Book> books;
//DATA
public String name;
public Integer age;
}
Book.java
package com.ivoronline.springboot_relationships_manytoone_jointable.entities;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import java.util.Set;
@Entity
public class Book {
//PRIMARY KEY
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer id;
//RELATIONSHIP
@ManyToMany(mappedBy="books")
public Set<Author> authors;
//DATA
public String title;
}
MyController.java
package com.ivoronline.springboot_relationships_manytoone_jointable.controllers;
import com.ivoronline.springboot_relationships_manytoone_jointable.entities.Author;
import com.ivoronline.springboot_relationships_manytoone_jointable.entities.Book;
import com.ivoronline.springboot_relationships_manytoone_jointable.repositories.AuthorRepository;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.HashSet;
@Controller
public class MyController {
@Autowired
AuthorRepository authorRepository;
@ResponseBody
@RequestMapping("/AddAuthor")
public String addAuthor() {
//CREATE BOOKS
Book book1 = new Book();
book1.title = "Book about dogs";
Book book2 = new Book();
book2.title = "Book about cats";
//CREATE AUTHORS
Author author1 = new Author();
author1.name = "John";
author1.age = 20;
Author author2 = new Author();
author2.name = "Bill";
author2.age = 30;
//ADD BOOKS TO AUTHORS
author1.books = new HashSet<Book>();
author1.books.add(book1);
author1.books.add(book2);
author2.books = new HashSet<Book>();
author2.books.add(book1);
author2.books.add(book2);
//STORE AUTHORS & BOOKS
authorRepository.save(author1);
authorRepository.save(author2);
//RETURN SOMETHING TO BROWSER
return "Author & Books were stored into DB";
}
}
R
R
e
e
s
s
u
u
l
l
t
t
s
s
http://localhost:8080/AddAuthor
AUTHOR BOOK http://localhost:8080/h2-console
AUTHOR_BOOKS
Application Structure
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>